{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [],
   "source": [
    "import numpy as np\n",
    "import pandas as pd\n",
    "from sklearn import metrics\n",
    "import matplotlib.pyplot as plt\n",
    "from sklearn.metrics import confusion_matrix\n",
    "from sklearn.neural_network import MLPClassifier\n",
    "data_train = pd.read_csv('engineered_data_train.csv')\n",
    "data_test = pd.read_csv('engineered_data_test.csv')\n",
    "\n",
    "X_train = data_train.drop('Y', axis=1)\n",
    "y_train = data_train['Y']\n",
    "\n",
    "X_test = data_test.drop('Y', axis=1)\n",
    "y_test = data_test['Y']"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "\"The default solver ‘adam’ works pretty well on relatively large datasets (with thousands of training samples or more) in terms of both training time and validation score. For small datasets, however, ‘lbfgs’ can converge faster and perform better.\""
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "metadata": {},
   "outputs": [],
   "source": [
    "from sklearn.model_selection import train_test_split\n",
    "X_train_valid, X_test_valid, y_train_valid, y_test_valid = train_test_split(X_train, y_train, test_size=0.3, random_state=49)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "metadata": {},
   "outputs": [],
   "source": [
    "learning_rate_init=[0.001,0.01,0.1]\n",
    "epsilon=[1e-8,1e-7,1e-6]\n",
    "alpha=[0.0001,0.01]\n",
    "beta_1=[0.9]\n",
    "beta_2=[0.999]\n",
    "maxAuc=0\n",
    "learningRate_=0\n",
    "epsilon_=0\n",
    "alpha_=0"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 17,
   "metadata": {},
   "outputs": [],
   "source": [
    "for lr in learning_rate_init:\n",
    "    for eps in epsilon:\n",
    "        for b_1 in beta_1:\n",
    "            for b_2 in beta_2:\n",
    "                for a in alpha:\n",
    "                    nn = MLPClassifier(solver='adam', alpha=a, hidden_layer_sizes=(5, 3), random_state=1, epsilon=eps, learning_rate_init=lr, beta_1=b_1, beta_2=b_2 ).fit(X_train_valid, y_train_valid)\n",
    "                    nn_probs = nn.predict_proba(X_test_valid)[:, 1]\n",
    "                    fpr, tpr, _ = metrics.roc_curve(y_test_valid, nn_probs)\n",
    "                    auc = metrics.auc(fpr, tpr)\n",
    "                    if auc>maxAuc:\n",
    "                        maxAuc=auc\n",
    "                        learningRate_=lr\n",
    "                        alpha_=a\n",
    "                        epsilon_=eps"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 19,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "0.9905436073943343 0.001 0.0001 1e-08\n"
     ]
    }
   ],
   "source": [
    "print(maxAuc,learningRate_,alpha_,epsilon_)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 23,
   "metadata": {},
   "outputs": [],
   "source": [
    "nn = MLPClassifier(solver='adam', alpha=0.0001, hidden_layer_sizes=(10, 5), random_state=1, epsilon=1e-8, learning_rate_init=0.001).fit(X_train_valid, y_train_valid)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 24,
   "metadata": {},
   "outputs": [],
   "source": [
    "nn_probs = nn.predict_proba(X_test_valid)[:, 1]\n",
    "fpr, tpr, _ = metrics.roc_curve(y_test_valid, nn_probs)\n",
    "auc = metrics.auc(fpr, tpr)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 28,
   "metadata": {
    "scrolled": true
   },
   "outputs": [],
   "source": [
    "layer1=[5,10,15,20,25]\n",
    "layer2=[5,10,15,20,25]\n",
    "maxAuc=0\n",
    "l1=0\n",
    "l2=0\n",
    "for a in layer1:\n",
    "    for b in layer2:\n",
    "        nn = MLPClassifier(solver='adam', alpha=0.0001, hidden_layer_sizes=(a, b), random_state=1, epsilon=1e-8, learning_rate_init=0.001).fit(X_train_valid, y_train_valid)\n",
    "        nn_probs = nn.predict_proba(X_test_valid)[:, 1]\n",
    "        fpr, tpr, _ = metrics.roc_curve(y_test_valid, nn_probs)\n",
    "        auc = metrics.auc(fpr, tpr)\n",
    "        if maxAuc<auc:\n",
    "            maxAuc=auc\n",
    "            l1=a\n",
    "            l2=b"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 31,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "0.9977323815815514 25 20\n"
     ]
    }
   ],
   "source": [
    "print(maxAuc, l1, l2)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 32,
   "metadata": {},
   "outputs": [],
   "source": [
    "nn = MLPClassifier(solver='adam', alpha=0.0001, hidden_layer_sizes=(25, 20), random_state=1, epsilon=1e-8, learning_rate_init=0.001).fit(X_train_valid, y_train_valid)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 33,
   "metadata": {},
   "outputs": [],
   "source": [
    "nn_probs = nn.predict_proba(X_test_valid)[:, 1]\n",
    "fpr, tpr, _ = metrics.roc_curve(y_test_valid, nn_probs)\n",
    "auc = metrics.auc(fpr, tpr)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 34,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "0.9977323815815514"
      ]
     },
     "execution_count": 34,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "auc"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 35,
   "metadata": {},
   "outputs": [],
   "source": [
    "nn_probs = nn.predict_proba(X_test)[:, 1]\n",
    "fpr, tpr, _ = metrics.roc_curve(y_test, nn_probs)\n",
    "auc = metrics.auc(fpr, tpr)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 36,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "0.9022103314744931"
      ]
     },
     "execution_count": 36,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "auc"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.7.3"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
